home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / phoonsrc / utils / p4doublepixel.c
C/C++ Source or Header  |  1992-09-20  |  2KB  |  109 lines

  1. #include<stdio.h>
  2. #include<fcntl.h>
  3.  
  4. typedef struct {
  5.   int fd;
  6.   int size;
  7.   int curr;
  8.   unsigned char buf[BUFSIZ];
  9. } *ReadChar, ReadCharItem;
  10.  
  11. #define readChar_alloc() (ReadChar)malloc(sizeof(ReadCharItem))
  12.  
  13. #define readChar_init(rc, _fd) { rc->size = rc->curr = 0; rc->fd = _fd; }
  14.  
  15. #define readChar(rc) (int)(rc->size == rc->curr? \
  16.                      (rc->size = read(rc->fd, rc->buf, BUFSIZ))? \
  17.                    rc->buf[(rc->curr = 1) - 1]: \
  18.                    (rc->curr = 0) - 1: \
  19.                      rc->buf[rc->curr++])
  20.  
  21. #define readChar_free(rc) free(rc);
  22.  
  23. #define DIGITS 4
  24.  
  25. char dp[] = { 0x03, 0x0c, 0x30, 0xc0 };
  26. char bit[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };  
  27.  
  28.  
  29. main(argc, argv)
  30.      int argc;
  31.      char *argv[];
  32. {
  33.   int fd;
  34.   ReadChar rc;
  35.   char xsize[DIGITS], ysize[DIGITS];
  36.   int i, c;
  37.   int w = 0, h = 0;
  38.   
  39.   if (argc > 1) {
  40.     if ((fd = open(argv[1], O_RDONLY)) == NULL)
  41.       { perror("open"); exit(0); }
  42.     dup2(fd, 0);
  43.   }
  44.  
  45.   rc = readChar_alloc();
  46.   readChar_init(rc, 0);
  47.   
  48.   if (readChar(rc) != 'P' || readChar(rc) != '4' || readChar(rc) != '\n') {
  49.     fputs("Unknown file format\n", stderr);
  50.     exit(0);
  51.   }
  52.   i = 0;
  53.   while ((c = readChar(rc)) != ' ' && i < DIGITS)
  54.     xsize[i++] = c;
  55.   if (c != ' ') {
  56.     fputs("Picture is too wide\n", stderr);
  57.     exit(0);
  58.   }
  59.   switch(i) {
  60.   case 4:
  61.     w = w + (xsize[i - 4] - '0') * 1000;
  62.   case 3:
  63.     w = w + (xsize[i - 3] - '0') * 100;
  64.   case 2:
  65.     w = w + (xsize[i - 2] - '0') * 10;
  66.   case 1:
  67.     w = w + (xsize[i - 1] - '0') * 1;
  68.   }
  69.   i = 0;
  70.   while ((c = readChar(rc)) != '\n' && i < DIGITS)
  71.     ysize[i++] = c;
  72.   if (c != '\n') {
  73.     fputs("Picture is too tall\n", stderr);
  74.     exit(0);
  75.   }
  76.   switch(i) {
  77.   case 4:
  78.     h = h + (ysize[i - 4] - '0') * 1000;
  79.   case 3:
  80.     h = h + (ysize[i - 3] - '0') * 100;
  81.   case 2:
  82.     h = h + (ysize[i - 2] - '0') * 10;
  83.   case 1:
  84.     h = h + (ysize[i - 1] - '0') * 1;
  85.   }
  86.   printf("P4\n%d %d\n", w * 2, h);
  87.  
  88.   while(h-- > 0) {
  89.     int j = 0, d = 0;
  90.     for (i = 0; i < w; i++) {
  91.       if (j == 0) {
  92.     j = 8;
  93.     c = readChar(rc);
  94.       }
  95.       j--;
  96.       if (c & bit[j])
  97.     d |= dp[j & 3];
  98.  
  99.       if ((j & 3) == 0) {
  100.     putchar(d);
  101.     d = 0;
  102.       }
  103.     }
  104.     if ((j & 3) != 0)
  105.       putchar(d);
  106.   }
  107.   fclose(stdout);
  108. }
  109.